package edu.mis.modules.member;

import java.util.Date;

import com.jfinal.core.Controller;

import edu.mis.model.Customer;
import edu.mis.model.Member;
import edu.mis.model.Recharge;
import edu.mis.modules.customer.CustomerService;
import edu.mis.modules.project.ProjectService;
import edu.mis.modules.recharge.RechargeService;
import edu.mis.modules.user.UserService;

public class MemberController extends Controller{
	
	static UserService userService = new UserService();
	static MemberService memberService = new MemberService();
	static CustomerService customerService = new CustomerService();
	static RechargeService rechargeService = new RechargeService();
	static ProjectService projectService = new ProjectService();
			
	
	public void index() {
		if(userService.findUserWithRole().equals(1)) {
			render("member.html");
		}else if(userService.findUserWithRole().equals(2)){
			render("cashierMember.html");
		}else {
			render("noAllAuthority.html");
		}
	}
	
	/**
	 * 会员列表
	 */
	
	public void list(){   
		setAttr("data", memberService.findAllMemberWithUserName());
		renderJson();   
	}
	
	/**
	 * 会员充值视图
	 */
	public void auth() {
		setAttr("project",projectService.findAllProject());
		setAttr("login",userService.findLoginUser());
		setAttr("member", memberService.findMemberByMemberId(getParaToInt("memberId")));
	}
	
	/**
	 * 会员充值
	 */
	public void recharge() {
		Recharge recharge = getModel(Recharge.class);
		Member member = memberService.findMemberByMemberId(recharge.getMemberId());
		recharge.setTime(new Date());
		recharge.setUserId(userService.findLoginUser().getUserId());
		int count = recharge.getCount();
		if(recharge.getProjectId()!=-1) {
			if(recharge.getCount()==null || recharge.getCount()==0) {
				
				setAttr("result", false);
				setAttr("msg", "会员充值失败，服务次数为空或者0!");
				
			}else {
				int price = projectService.findProjectByProjectId(recharge.getProjectId()).getProjectPrice();
				recharge.setTotal(price*recharge.getCount());
				int a = count/10;
				member.setMemberScore(member.getMemberScore()+60*a);
				recharge.setCount(count+a);
				
				if(rechargeService.saveRecharge(recharge)==true && memberService.updateMember(member)==true) {
					
					setAttr("result", true);
				    setAttr("msg", "会员充值成功!");
				}else {
					setAttr("result", false);
				    setAttr("msg", "会员充值失败，系统错误!");
				}
				
			}
			
		}else {
			
			setAttr("result", false);
			setAttr("msg", "会员充值失败，充值服务为空!");
		}
		renderJson();   
	}
	
	
	
	/**
	 * 删除会员
	 * 
	 */
	public void delete() {
		int memberId = getParaToInt("memberId");
		Customer customer = customerService.findCustomerByCustomerNumber(memberService.findMemberByMemberId(memberId).getCustomerNumber());
		if(memberService.deleteMemberByMemberId(memberId)){
			customer.setVip(2);
			if(customerService.updateCustomer(customer)) {
				setAttr("result", true);
				setAttr("msg", "删除会员成功!");
			}else {
				setAttr("result", false);
				setAttr("msg", "删除会员失败!");
			}
			
		}else{
			setAttr("result", false);
			setAttr("msg", "删除会员失败!");
		}
		renderJson();
	}
	
}
